package br.com.cinesystem.persistence;

import br.com.cinesystem.entity.Endereco;



public class EnderecoDao extends Dao {

    public EnderecoDao(){

    }

    public Endereco salvar(Endereco e) throws Exception{
        abrirConexao();
        stmt = con.prepareStatement("insert into endereco values(null,?,?,?,?,?,?,?)");
        stmt.setString(1, e.getLogradouro());
        stmt.setString(2, e.getNumero());
        stmt.setString(3, e.getComplemento());
        stmt.setString(4, e.getCep());
        stmt.setString(5, e.getBairro());
        stmt.setString(6, e.getCidade());
        stmt.setString(7, e.getEstado());
        stmt.execute();

        stmt = con.prepareStatement("select LAST_INSERT_ID() as id");

        rs = stmt.executeQuery();
        
        Endereco end = null;

        if(rs.next()){
            end = new Endereco();
            end.setIdEndereco(rs.getInt("id"));
        }
        fecharConexao();
        return end;

    }

    public Endereco alterar(Endereco e)throws Exception{
        abrirConexao();
        stmt = con.prepareStatement("UPDATE endereco SET logradouro=?, numero=?, complemento=?, cep=?," +
                " bairro=?, cidade=?, estado=? WHERE idEndereco=?");
        stmt.setString(1, e.getLogradouro());
        stmt.setString(2, e.getNumero());
        stmt.setString(3, e.getComplemento());
        stmt.setString(4, e.getCep());
        stmt.setString(5, e.getBairro());
        stmt.setString(6, e.getCidade());
        stmt.setString(7, e.getEstado());
        stmt.setInt(8, e.getIdEndereco());

        stmt.execute();
        fecharConexao();
        return e;
    }
    public Endereco buscar(Endereco end)throws Exception{
        abrirConexao();
        stmt = con.prepareStatement("SELECT * FROM endereco WHERE idEndereco=?");
        stmt.setInt(1, end.getIdEndereco());

        rs = stmt.executeQuery();

        Endereco e = null;

        if(rs.next()){
            e = new Endereco();
            e.setLogradouro(rs.getString("logradouro"));
            e.setNumero(rs.getString("numero"));
            e.setComplemento(rs.getString("complemento"));
            e.setCep(rs.getString("cep"));
            e.setBairro(rs.getString("bairro"));
            e.setCidade(rs.getString("cidade"));
            e.setEstado(rs.getString("estado"));
        }
        fecharConexao();
        return e;
    }
    public Endereco pesquisarEndereco(Endereco e) throws Exception{
        abrirConexao();
        stmt = con.prepareStatement("select * from endereco where logradouro=? and numero=? and complemento=?"
                + " and cep=? and bairro=? and cidade=? and estado=?");
        stmt.setString(1, e.getLogradouro());
        stmt.setString(2, e.getNumero());
        stmt.setString(3, e.getComplemento());
        stmt.setString(4, e.getCep());
        stmt.setString(5, e.getBairro());
        stmt.setString(6, e.getCidade());
        stmt.setString(7, e.getEstado());


        rs = stmt.executeQuery();

        Endereco endereco = null;

        if(rs.next()){
            endereco = new Endereco();
            endereco.setIdEndereco(rs.getInt("idEndereco"));
        }
        fecharConexao();
        return endereco;
    }

}
